package com.datang.pet.api.petController;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-06-21 18:39
 * @notify
 * @version 1.0
 */

import com.datang.pet.common.redis.RedisTool;
import com.datang.pet.common.response.Result;
import com.datang.pet.common.utils.MD5;
import com.datang.pet.common.utils.StringUtils;
import com.datang.pet.pojo.Pet;
import com.datang.pet.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UpdatePetController {

    @Autowired
    private PetService petService;

    @Autowired
    private RedisTool redisTool;

    @PostMapping ("/updatePet")
    public Result updatePet(@RequestBody Pet pet) throws Exception {
        if (!StringUtils.isNUll(pet.getId())) {
            throw new Exception("id为空");
        }
        try {
            pet.setBelongUserId(MD5.decryptData(pet.getBelongUserId()));
        } catch (Exception e) {
            throw new Exception("userId解密出错！");
        }
        petService.updatePet(pet);
        redisTool.remove("getAllPetByUserId"+pet.getBelongUserId());
        return Result.ok("ok");
    }
}
